<template>
  <Panel>
    <!-- 标题 -->
    <template #title>账号列表</template>
    <!-- 内容 -->
    <template>
      <!-- 按钮 -->
      <div>
        <el-button type="danger" size="mini" @click="delAll">批量删除</el-button>
        <el-button type="primary" size="mini" @click="toggleSelection()">取消选择</el-button>
      </div>
      <!-- 表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        size:="mini"
        type:="index"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="account" label="账号">
        </el-table-column>
        <el-table-column prop="userGroup" label="管理权限">
        </el-table-column>
        <el-table-column label="创建时间">
          <template v-slot="{ row }">
            {{ row.ctime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
        <template v-slot= {row}>
          <el-button
          type="primary"
          size="mini"
          @click="showItem(row)">
          编辑
        </el-button>
        <el-button
          @click.native.prevent="delite(row.id)"
          type="danger"
          size="mini">
          移除
        </el-button>
      </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      size:="mini"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 弹出框提示 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="40%">
      <!-- 布局盒子 -->
      <div>
        <el-form
          :model="editForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          size="mini"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              type="text"
              v-model="editForm.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="userGroup">
            <el-select v-model="editForm.userGroup" placeholder="请选择">
              <el-option label="普通管理员" value="普通管理员"> </el-option>
              <el-option label="超级管理员" value="超级管理员"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部盒子 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { userList, remove, allremove, cha } from '@/api/user.js'
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogVisible: false,
      // 表单数据
      editForm: {
        account: '',
        userGroup: ''
      },
      // 规则数据
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        userGroup: [
          { required: true, message: '请选择管理员权限', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    // 实例准备好了就开始发请求
    this.getUsers()
  },
  methods: {
    // 获取数据列表
    async getUsers() {
      const { data, total } = await userList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
    },
    // 编辑
    showItem(row) {
      this.editForm = { ...row }
      this.dialogVisible = true
    },
    // 编辑中的确定
    editUser(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg } = await cha({
            id: this.editForm.id,
            account: this.editForm.account,
            userGroup: this.editForm.userGroup
          })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
            this.dialogVisible = false
            this.getUsers()
          }
        } else {
          return false
        }
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getUsers()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getUsers()
    },
    // 删除
    delite(row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { code, msg } = await remove({ id: row })
        if (code === 0) {
          this.$message({
            type: 'success',
            message: msg
          })
          this.getUsers()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 批量删除
    delAll() {
      const ids = []
      this.multipleSelection.forEach(item => {
        ids.push(item.id)
      })
      // 数组为空不往下执行
      if (ids.length === 0) return
      this.$confirm('此操作将永久删除所选用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { code, msg } = await allremove({ ids: JSON.stringify(ids) })
        if (code === 0) {
          this.$message({
            type: 'success',
            message: msg
          })
          this.getUsers()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-table{
  margin: 20px 0 40px 0
}
.el-form-item{
  width: 293px;
}
</style>
